﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLCV
{
    public partial class frmLoaiDi : Form
    {        
        public frmLoaiDi()
        {
            InitializeComponent();
        }

        private void btnthemdi_Click(object sender, EventArgs e)
        {
            try
            {
                LoaiDiDTO _ThemLoaiDi = new LoaiDiDTO();
                _ThemLoaiDi.Ten = txtLoaiMoiDi.Text.Trim();
                if (LoaiDiBUS.Add(_ThemLoaiDi))
                {
                    txtLoaiMoiDi.Text = "";
                    Reload();
                }
                else
                {
                    MessageBox.Show("Không thể thêm loại mới.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
        
        private void frmLoaiDi_Load(object sender, EventArgs e)
        {
            btnLuuDi.Enabled = false;
            btnThemDi.Enabled = false;
            Reload();
        }

        private void btnLuuDi_Click(object sender, EventArgs e)
        {
            try
            {
                LoaiDiDTO _SuaLoaiDi = (LoaiDiDTO)lsDSLoaiDi.Items[lsDSLoaiDi.SelectedIndex];
                _SuaLoaiDi.Ten = txtTenLoaiMoiDi.Text.Trim();
                if (LoaiDiBUS.Alter(_SuaLoaiDi))
                {
                    Reload();
                }
                else
                {
                    MessageBox.Show("Không thể thay đổi thông tin loại.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
        void Reload()
        {
            lsDSLoaiDi.DataSource = LoaiDiBUS.LoadAll();
            lsDSLoaiDi.DisplayMember = "TEN";
            lsDSLoaiDi.ValueMember = "ID";
            lsDSLoaiDi.SelectedIndex = -1;
        }
        private void btnThoatDi_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtLoaiMoiDi_TextChanged(object sender, EventArgs e)
        {
            if (txtLoaiMoiDi.Text.Trim() == "")
            {
                btnThemDi.Enabled = false;
            }
            else
            {
                btnThemDi.Enabled = true;
            }
        }

        private void txtTenLoaiMoiDi_TextChanged(object sender, EventArgs e)
        {
            if (txtTenLoaiMoiDi.Text.Trim() == "" || lsDSLoaiDi.SelectedIndex == -1)
            {
                btnLuuDi.Enabled = false;
            }
            else
            {
                btnLuuDi.Enabled = true;
            }
        }

        private void lsDSLoaiDi_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (lsDSLoaiDi.SelectedIndex == -1)
            {
                txtTenLoaiMoiDi.Text = "";
                txtTenLoaiMoiDi.Enabled = false;
                btnLuuDi.Enabled = false;
                return;
            }
            txtTenLoaiMoiDi.Enabled = true;
            btnLuuDi.Enabled = true;
            txtTenLoaiMoiDi.Text = ((LoaiDiDTO)lsDSLoaiDi.Items[lsDSLoaiDi.SelectedIndex]).Ten;
        }

    }
}
